package com.wanxi.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

@WebServlet("/getCookie")
public class CookieGetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        Cookie[] cookies = req.getCookies();
        for (Cookie c : cookies
        ) {
            if (c.getName().equals("username")) {
                System.out.println("name:" + c.getName() + " value:" + URLDecoder.decode(c.getValue(),"UTF-8"));
                System.out.println("过期时间 " + c.getMaxAge());
            }
        }
        System.out.println(req.getSession().getAttribute("sessionUsername"));
    }
}
